package com.wq.concurrency.basic;

/**
 * @Author: wangqiang20995
 * @Date:19-1-27
 * @Description:测试父线程和子线程的threadlocal不能互相传值
 * @Resource:
 */
public class ThreadLocalDemo {

    private ThreadLocal<String> threadLocal = new ThreadLocal<>();

    private ThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    public void setThreadLocalValue(String value) {
        this.threadLocal.set(value);
    }

    public void setInheritLocalValue(String value) {
        this.inheritableThreadLocal.set(value);
    }

    public static void main(String args[]) {

        ThreadLocalDemo threadLocalDemo = new ThreadLocalDemo();

//        threadLocalDemo.setThreadLocalValue("HelloWorld from parent thread and stored in ThreadLocal");
//
//        threadLocalDemo.setInheritLocalValue("HelloWorld from parent thread and stored in InheritThreadLocal");

//        Thread subThread = new Thread(() -> {
//            System.out.println("从父线程的threadLocal里面取值-->" + threadLocalDemo.threadLocal.get());
//
//            System.out.println("从父线程的inheritThreadLocal里面取值-->" + threadLocalDemo.inheritableThreadLocal.get());
//        });

//        subThread.start();

        Thread thread = new Thread(()->{
            for(int i = 0;i<20;i++) {
                ThreadLocal<String> local = new ThreadLocal<>();
//                ThreadLocal<String> hello = new ThreadLocal<>();

                local.set("local-" + i);
//                hello.set("hello");
            }

        });
        thread.start();



    }
}
